﻿[//lasso
/*----------------------------------------------------------------------------

[response_header]
A type to get and set HTTP response header values.

Author: Jason Huck
Last Modified: Feb. 27, 2007
License: 

Description:
This is a custom type for getting and setting values in the HTTP response 
header. It can be useful for adding additional parameters such as "Safe", 
overriding values such as "Server", or changing the HTTP response code to 
something slightly more accurate than a generic 200 OK.


Instance Variables:

->original - A copy of the original header generated by Lasso, before any 
modifications are made using this type.

->response - The first line of the header, containing the HTTP response code.

->headers - An array of name/value pairs representing individual lines within 
the overall response header.

->post - An array of name/value pairs representing POST arguments to be 
included with the response header. (Note that these arguments will be ignored 
if the HTTP response code is set to a value that does not cause the browser to 
expect them.)


Methods:

->set - Accepts a name/value pair, and updates both the 'headers' array and 
the actual content_header accordingly.

->response - Gets or sets the response line (i.e., 'HTTP/1.0 200 OK'), 
depending on whether an argument was passed.

->restore - Restores the header to its original value. Useful for 
testing/debugging.

->retrieve - Used internally on create, populates the 'headers' instance 
variable.

->output - Used internally, assembles a new header using the current values.


Sample Usage:
var('hdr') = response_header;	
$hdr->response = 'HTTP/1.1 303 See Other';
$hdr->set('Location' = 'http://www.mydomain.com/something.html');
$hdr->set('Safe' = 'yes');

/+ would cause the page to return the following header:

HTTP/1.1 303 See Other
Server: Lasso/8
MIME-Version: 1.0
Content-type: text/html; charset=UTF-8
Location: http://www.mydomain.com/something.html
Safe: yes

+/

Downloaded from tagSwap.net on Nov. 11, 2008.
Latest version available from <http://tagSwap.net/response_header>.

----------------------------------------------------------------------------*/
		

define_type(
	'header',
	-namespace='response_',
	-description='A type to get and set HTTP response header values.'
);
	local(
		'original' = string,
		'response' = string,
		'headers' = array,
		'post' = array
	);

	define_tag('onCreate');
		self->'original' = string(content_header);
		self->retrieve;
	/define_tag;

	define_tag(
		'retrieve',
		-description='Returns an array of name/value pairs from the response header.'
	);
		local('out') = array;
		
		iterate(content_header->split('\r\n'), local('line'));
			if(loop_count == 1);
				self->response = #line;
			else(#line->size);		
				local('name') = #line->split(':')->first;
				local('value') = #line->split(':')->second;
				#name->trim;
				#value->trim;
				self->'headers'->insert(#name = #value);
			/if;
		/iterate;
	/define_tag;
	
	define_tag('output');
		local('out') = self->response + '\r\n';
		
		iterate(self->'headers', local('i'));
			#out += #i->first + ': ' + #i->second + '\r\n';
		/iterate;
		
		if(self->'post'->size);
			#out += '\r\n';
			
			iterate(self->'post', local('i'));
				#out += #i->first + '=' + #i->second + '&';
			/iterate;
			
			#out->removetrailing('&')&append('\r\n');
		/if;
		
		return(#out);
	/define_tag;
	
	define_tag('set', -req='pair', -type='pair');
		local('name' = @#pair->first, 'value' = @#pair->second);
	
		self->headers >> #name ? 
			self->'headers'->find(#name)->first->second = #value 
			| self->'headers'->insert(#name = #value);
			
		content_header = self->output;
	/define_tag;
	
	define_tag('response', -opt='value');
		if(local_defined('value'));
			self->'response' = #value;
			content_header = self->output;
		else;
			return(self->'response');
		/if;
	/define_tag;
	
	define_tag('restore');
		content_header = self->'original';
	/define_tag;
	
	define_tag('onConvert');
		return(self->output);
	/define_tag;		
/define_type;
]